package com.huidian.gitbatchprocessing.ui.mycomponent;

import org.eclipse.jgit.lib.Repository;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义表格存储对象
 *
 * @author zxb
 * @date 2021/6/17
 */
public class CheckBoxTableModel extends AbstractTableModel {
    /**
     * 表头数据
     */
    private final List<String> columnHeadNames;
    /**
     * 行数据
     */
    private List<List<Object>> rowDataList;

    public CheckBoxTableModel(List<String> columnHeadNames) {
        this(columnHeadNames, new ArrayList<>());
    }

    public CheckBoxTableModel(List<String> columnHeadNames, List<List<Object>> rowDataList) {
        this.columnHeadNames = columnHeadNames;
        this.rowDataList = rowDataList;
    }

    public List<List<Object>> getRowDataList() {
        return rowDataList;
    }

    public void setRowDataList(List<List<Object>> rowDataList) {
        this.rowDataList = rowDataList;
    }

    public List<String> getColumnHeadNames() {
        return columnHeadNames;
    }

    @Override
    public int getColumnCount() {
        return columnHeadNames.size();
    }

    @Override
    public String getColumnName(int column) {
        return columnHeadNames.get(column);
    }

    @Override
    public int getRowCount() {
        return rowDataList.size();
    }

    @Override
    public Object getValueAt(int row, int column) {
        List<Object> objects = rowDataList.get(row);
        Object obj = objects.get(column);
        return obj instanceof Repository ? ((Repository) obj).getWorkTree().getName() : obj;
    }

    @Override
    @SuppressWarnings("rawtypes")
    public Class getColumnClass(int column) {
        return (getValueAt(0, column).getClass());
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        List<Object> objects = rowDataList.get(row);
        objects.set(column, value);
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return true;
    }

    public void selectAllOrNull(boolean value) {
        for (int i = 0; i < getRowCount(); i++) {
            this.setValueAt(value, i, 0);
        }
    }
}